package com.cheyun.netsalev3.view.home

import android.content.Intent
import android.util.Log
import android.view.View
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.GridLayoutManager
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.MyAdapter
import com.cheyun.netsalev3.databinding.HomeTaskFragment2Binding
import com.cheyun.netsalev3.utils.*
import com.cheyun.netsalev3.view.OverdueActivity
import com.cheyun.netsalev3.view.PassengerRegisterActivity
import com.cheyun.netsalev3.viewmodel.HomeTaskFragment2ViewModel
import com.gyf.immersionbar.ImmersionBar
import com.jeremyliao.liveeventbus.LiveEventBus
import java.util.*


class HomeTaskFragment2 : BaseFragment<HomeTaskFragment2Binding>() {
    override fun initContentView(): Int {
        return R.layout.home_task_fragment2
    }

    override fun initViewModel(): ViewModel? {
        var factory = InjectorUtils.getHomeTaskFragment2ViewModelFactory()
        return ViewModelProviders.of(this, factory).get(HomeTaskFragment2ViewModel::class.java)
    }
    private lateinit var adapter: MyAdapter

    private var listFragmet = ArrayList<Fragment>()
    override fun initView() {
        binding.recyclerView.layoutManager = GridLayoutManager(context, 3)
        adapter = MyAdapter(R.layout.home_report_item, binding.viewModel)
        binding.recyclerView.addItemDecoration(SpaceItemDecoration(this.context, 8, 8, 3))
        binding.recyclerView.adapter = adapter

//        binding.tabLayout.setupWithViewPager(binding.viewPager)
//        binding.tabLayout.setSelectedTabIndicator(R.drawable.circular_red)
//        binding.tabLayout.setSelectedTabIndicatorHeight(AppCongfig.width / 7)
//        binding.viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
//            override fun onPageScrollStateChanged(state: Int) {
//
//            }
//
//            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
//            }
//
//            override fun onPageSelected(position: Int) {
//                binding.viewPager.adapter?.count?.apply {
//                    for (i in 0 until binding.viewPager.adapter!!.count) {
//                        var item = binding.tabLayout.getTabAt(i)
//                        if (position == i) {
//                            item?.customView?.findViewById<TextView>(R.id.dayText)?.setTextColor(ContextCompat.getColor(context!!, R.color.white))
//                            item?.customView?.findViewById<TextView>(R.id.DayText)?.setTextColor(ContextCompat.getColor(context!!, R.color.white))
//                        } else {
//                            item?.customView?.findViewById<TextView>(R.id.dayText)?.setTextColor(ContextCompat.getColor(context!!, R.color.data_day2_nor))
//                            item?.customView?.findViewById<TextView>(R.id.DayText)?.setTextColor(ContextCompat.getColor(context!!, R.color.data_day_nor))
//                        }
//                    }
//                }
////                binding.viewModel?.changePosition(position)
//            }
//        })


//        binding.outLayout.setOnClickListener {
//            var intent = Intent(context, OverdueActivity::class.java)
//            //逾期state：1 七日强转2
//            intent.putExtra("state", 1)
//            startActivity(intent)
//        }
        binding.strongTurnBut.setOnClickListener {
            var intent = Intent(context, OverdueActivity::class.java)
            //逾期state：1 七日强转2
            intent.putExtra("state", 2)
            startActivity(intent)
        }
        binding.keliuLayout.setOnClickListener {
            //非VIP版本显示功能   工作台的“今日客流”显示不点击
            var hasAuthorization = MySharedPreferences.hasAuthorization()
            if(hasAuthorization){
                //今日客流
//                var intent = Intent(context, PassengerReceptionActivity::class.java)
//                intent.putExtra("currentDay", true)
//                startActivity(intent)

                var intent = Intent(context, PassengerRegisterActivity::class.java)
                startActivity(intent)


            }else{
                ToastUtil.toastShortMessage("此功能只对VIP用户开启")
            }
        }

//        binding.missTelLayout.setOnClickListener {
//            //未接来电
//            var intent = Intent(context, MissedTelActivity::class.java)
//            startActivity(intent)
//        }
//        binding.driveblockLayout.setOnClickListener {
//            //试驾列表 --> 下发线索
//            var intent = Intent(context, AllocatedActivity::class.java)
//            startActivity(intent)
//        }

//        binding.swipeRefreshLayout.setOnRefreshListener {
//            binding.swipeRefreshLayout.isRefreshing = true
//            binding.viewModel?.refresh()
//        }

//        binding.viewModel?.networkState?.observe(this, Observer {
//            Log.i(TAG,"networkState:"+it)
//            binding.swipeRefreshLayout.isRefreshing = it == NetworkState.LOADING
//        })
//        binding.swipeRefreshLayout.setIsScrollDragger(false)

//        binding.lyscan.visibility = View.GONE
        var loginData = MySharedPreferences.getLoginData()
        loginData?.member?.apps?.receive?.apply {
            for (i in this) {
                //6 扫码权限
                if (i == 6) {
//                    binding.lyscan.visibility = View.VISIBLE
                }
            }
        }
        loginData?.member?.isadmin?.apply {
            if(this==1){
//                binding.lyscan.visibility = View.VISIBLE
            }
        }

    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        Log.e("setUserVisibleHint", isVisibleToUser.toString())
        if (userVisibleHint && isBindingInit()) {
            binding?.viewModel?.refreshMessage()
            binding?.viewModel?.getMoney()
        }
    }

    override fun onResume() {
        super.onResume()
        ImmersionBar.with(this).statusBarColor(R.color.colorbg_blue)
                .fitsSystemWindows(true)
                .init()
        Log.e("onResume", userVisibleHint.toString())
        if (userVisibleHint && isBindingInit()) {
            if(binding?.viewModel?.hasMessageCallback!!) {
                binding?.viewModel?.refreshMessage()
                binding?.viewModel?.getMoney()
            }
        }
    }

    override fun initData() {
        binding.viewModel?.reportDatas?.observe(this, Observer { list ->
            adapter.submitList(list)
            adapter.notifyDataSetChanged()
        })
        binding.viewModel?.calendarList?.observe(this, Observer { lists ->
            if (lists != null) {
                listFragmet.clear()
                for (i in lists.indices) {
                    listFragmet.add(HomeViewPageFragment.newInstance(i))
                }
                //初始化viewPager
//                val pagerAdapter = HomePageAdapter(childFragmentManager, listFragmet)
//                binding.viewPager.adapter = pagerAdapter
//                for (i in lists.indices) {
//                    var itemdata = lists[i]
//                    var itembinding = DateItemBinding.inflate(layoutInflater, null, false)
//                    itembinding.xinqiTex.text = FunctionUtils.calendarWeek(itemdata.get(Calendar.DAY_OF_WEEK))
//                    if (i == 0) {
//                        itembinding.dayText.text = "今"
//                    } else {
//                        itembinding.dayText.text = itemdata.get(Calendar.DAY_OF_MONTH).toString()
//                    }
//
//                    var time = LunarCalendar.solarToLunar(itemdata.get(Calendar.YEAR),
//                            itemdata.get(Calendar.MONTH) + 1,
//                            itemdata.get(Calendar.DAY_OF_MONTH)
//                    )
//                    itembinding.DayText.text = LunarCalendar.getChinaDate(time[2])
//
//                    if (0 == i) {
//                        itembinding.dayText.setTextColor(ContextCompat.getColor(context!!,R.color.white))
//                        itembinding.DayText.setTextColor(ContextCompat.getColor(context!!,R.color.white))
//                    } else {
//                        itembinding.dayText.setTextColor(ContextCompat.getColor(context!!,R.color.data_day2_nor))
//                        itembinding.DayText.setTextColor(ContextCompat.getColor(context!!,R.color.data_day_nor))
//                    }
//                    var item = binding.tabLayout.getTabAt(i)
//                    item?.customView = itembinding.root
//
//                }
//                IndicatorLineUtil.setIndicator(binding.tabLayout, AppCongfig.width / 7, FunctionUtils.dip2px(context!!, 0f))
            }
        }
        )

        binding.viewModel?.isHuafeiTipShow2?.observe(this, Observer {
            val para = binding.lyTop.layoutParams
            para.height = resources.getDimension(if(it) R.dimen.home_task_fragment1_huafei_dp else R.dimen.home_task_fragment1_dp).toInt()
            binding.lyTop.setLayoutParams(para)
        })
        //接受通知消息
        LiveEventBus.get()
                .with("notice", String::class.java)
                .observe(this, Observer {
                    binding?.viewModel?.refresh()
                })

//        LiveEventBus.get()
//                .with("enableScroll", Boolean::class.java)
//                .observe(this, Observer {
//                    binding?.swipeRefreshLayout?.setIsScrollDragger(it)
//                })
    }

    override fun lazyInit() {
        Log.i(TAG,"override lazyinit HomeTaskFragment2")
    }

}
